package com.rst.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Department;
import com.rst.model.Qualification;
import com.rst.model.Teacher;
import com.rst.service.DepartmentService;
import com.rst.service.QualificationService;
import com.rst.service.TeacherService;


@Controller
public class TeacherController {
	
	@Autowired
	TeacherService teacherService;

	@Autowired
	QualificationService qualificationService;
	
	@Autowired
	DepartmentService departmentService;


	@RequestMapping(value="/addTeacher", method=RequestMethod.GET)
	public ModelAndView addTeacher()
	{
		ModelAndView mav = new ModelAndView("teacher/addTeacher");
		Teacher teacher = new Teacher();
		mav.getModelMap().put("teacher", teacher);
		return mav;
	}
	
	@RequestMapping(value="/saveTeacher", method=RequestMethod.POST)
	public String saveTeacher(@ModelAttribute("teacher")Teacher teacher, BindingResult result, SessionStatus status)
	{
		teacherService.saveTeacher(teacher);
		status.setComplete();
		return "redirect:manageteacher.htm";
	}
	
	@RequestMapping(value="/editTeacher", method=RequestMethod.GET)
	public ModelAndView editTeacher(@RequestParam("id")Long teacherId)
	{
		ModelAndView mav = new ModelAndView("teacher/editTeacher");
		Teacher teacher = teacherService.getTeacherById(teacherId);
		mav.addObject("teacher", teacher);
		return mav;
	}
	

	@RequestMapping(value="/manageteacher", method=RequestMethod.GET)
	public ModelAndView manageteacher()
	{
		ModelAndView mav = new ModelAndView("teacher/manageTeacher");		
		List<Teacher> teacherList = teacherService.getTeacherList();
		mav.addObject("teacherList", teacherList);
		return mav;
	}

	@RequestMapping(value="/deleteTeacher", method=RequestMethod.GET)
	public String deleteTeacher(@RequestParam("id")Long teacherId )
	{
		String status = teacherService.deleteTeacher(teacherId);
		return "redirect:manageteacher.htm";
	}
	
	
	
	// for teacher qualification CRUD
	
	@RequestMapping(value="/addQualification", method=RequestMethod.GET)
	public ModelAndView addQualification()
	{
		ModelAndView mav = new ModelAndView("teacher/qualification/addQualification");
		Qualification qualification = new Qualification();
		mav.getModelMap().put("qualification", qualification);
		return mav;
	}
	
	@RequestMapping(value="/saveQualification", method=RequestMethod.POST)
	public String saveQualification(@ModelAttribute("qualification")Qualification qualification, BindingResult result, SessionStatus status)
	{
		qualificationService.saveQualification(qualification);
		status.setComplete();
		return "redirect:manageQualification.htm";
	}
	
	@RequestMapping(value="/editQualification", method=RequestMethod.GET)
	public ModelAndView editQualificationr(@RequestParam("id")Long qualificationId)
	{
		ModelAndView mav = new ModelAndView("teacher/qualification/editQualification");
		Qualification qualification = qualificationService.getQualificationById(qualificationId);
		mav.addObject("qualification", qualification);
		return mav;
	}
	

	@RequestMapping(value="/manageQualification", method=RequestMethod.GET)
	public ModelAndView managQualification()
	{
		ModelAndView mav = new ModelAndView("teacher/qualification/manageQualification");		
		List<Qualification> qualificationList = qualificationService.getQualificationList();
		mav.addObject("qualificationList", qualificationList);
		return mav;
	}

	@RequestMapping(value="/deleteQualification", method=RequestMethod.GET)
	public String deleteQualificationList(@RequestParam("id")Long qualificationId )
	{
		String status = qualificationService.deleteQualificationList(qualificationId);
		return "redirect:manageQualification.htm";
	}
	

	
	// for teacher department CRUD
	
	@RequestMapping(value="/addDepartment", method=RequestMethod.GET)
	public ModelAndView addDepartment()
	{
		ModelAndView mav = new ModelAndView("teacher/department/addDepartment");
		Department department = new Department();
		mav.getModelMap().put("department", department);
		return mav;
	}
	
	@RequestMapping(value="/saveDepartment", method=RequestMethod.POST)
	public String saveDepartment(@ModelAttribute("department")Department department, BindingResult result, SessionStatus status)
	{
		departmentService.saveDepartment(department);
		status.setComplete();
		return "redirect:manageDepartment.htm";
	}
	
	@RequestMapping(value="/editDepartment", method=RequestMethod.GET)
	public ModelAndView editDepartment(@RequestParam("id")Long departmentId)
	{
		ModelAndView mav = new ModelAndView("teacher/department/editDepartment");
		Department department = departmentService.getDepartmentById(departmentId);
		mav.addObject("department", department);
		return mav;
	}
	

	@RequestMapping(value="/manageDepartment", method=RequestMethod.GET)
	public ModelAndView manageDepartment()
	{
		ModelAndView mav = new ModelAndView("teacher/department/manageDepartment");		
		List<Department> departmentList = departmentService.getDepartmentList();
		mav.addObject("departmentList", departmentList);
		return mav;
	}

	@RequestMapping(value="/deleteDepartment", method=RequestMethod.GET)
	public String deleteDepartment(@RequestParam("id")Long departmentId)
	{
		String status = departmentService.deleteDepartment(departmentId);
		return "redirect:manageDepartment.htm";
	}
	

	
}
